<?php

require_once 'JSCasts/Exception.php';

/**
 * JSCasts Application Library
 *
 * @author Michael Girouard (mikeg@lovemikeg.com)
 */
class JSCasts
{
    private static $baseDir;
    private static $fileExtension;
    private static $defaultNameSpace;
    
    /**
     * Basic application configurator
     *
     * @todo This should be set up via an array
     * @todo There should be a defaults file
     */
    public static function configure(array $configuration)
    {
        self::$baseDir = dirname(__FILE__);
        self::$fileExtension = '.php';
        self::$defaultNameSpace = 'JSCasts';
        
        set_include_path(self::$baseDir . PATH_SEPARATOR . get_include_path());
        ini_set('error_reporting', E_ALL);
        ini_set('show_errors', 'yes');
    }
    
    /**
     * Loads any JSCasts package
     *
     * @param string $package
     * @todo This should probably be in a separate loader library.
     */
    public static function load($package)
    {
        $packagePath = preg_replace('/[^A-Z0-9]/i', DIRECTORY_SEPARATOR, $package);
        $path = self::packagePath($package);
        
        if (!is_file($path) && !preg_match('/^JSCasts/', $path)) {
            $path = self::packagePath($package, self::$defaultNameSpace);
        }
        
        if (!is_file($path)) {
            throw new JSCasts_Exception('Unable to load file: ' . $path);
        }
        
        require_once $path;
    }

    public static function translate($message)
    {
        return $message;
    }
    
    public static function say($message)
    {
        echo self::translate($message);
    }

    /**
     * Calculates a package's path and filename
     *
     * @param unknown_type $packageName
     * @return unknown
     */
    private static function packagePath($packageName, $nameSpace = '')
    {
        $packagePath = preg_replace('/[^A-Z0-9]/i', DIRECTORY_SEPARATOR, $packageName);
        
        if (empty($nameSpace)) {
            $nameSpace = self::$defaultNameSpace;
        }
        
        return 
            self::$baseDir . DIRECTORY_SEPARATOR .
            $nameSpace     . DIRECTORY_SEPARATOR .
            $packagePath   . self::$fileExtension;
    }
}